import http.client
import time
import datetime

# 自定义过滤器
from functools import wraps
from flask import session, g

# from app import db
# from app.models.models import User


def show_index_colorful(index):
    if index == 1:
        return "first"
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""


# 装饰器
def login_user_data2(view_func):
    from app2 import db
    from app2.models.models import User
    @wraps(view_func)  # 将原函数view_func的一些属性值带过来，如__name__
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id")
        user = db.session.query(User).filter(User.id == user_id).first()
        g.user = user
        return view_func(*args, **kwargs)
    return wrapper


def login_user_data(view_func):
    from app2.models.models import User
    from app2 import db

    @wraps(view_func)
    def wrapper(*args, **kwargs):
        # 尝试从session中获取user_id
        user_id = session.get('user_id')  # 获取不到，返回None
        user = None
        if user_id:
            # 用户已登录
            try:
                user = db.session.query(User).filter(User.id == user_id).first()
            except Exception as e:
                print("提取从User模型类查询，产生异常...")

        # 使用g变量临时保存user信息
        # g变量中保存的数据可以在请求开始到请求结束过程中的使用，我们将在需要函数中需要的数据，先存进去，然后再flask调用其他函数时，再通过g取出来，即可完成在多个函数中传递数据
        g.user = user  #

        return view_func(*args, **kwargs)

    return wrapper


# 获取网络时间
def get_webservertime():
    conn = http.client.HTTPConnection('www.baidu.com')
    conn.request("GET", "/")
    r = conn.getresponse()
    # r.getheaders() #获取所有的http头
    ts = r.getheader('date')  # 获取http头date部分
    # print(ts, 11111111111111111111111111)
    # 将GMT时间转换成北京时间
    ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
    # print(ltime)
    ttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60)


    # print(ttime)
    dat = "date %u-%02u-%02u" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday)
    tm = "time %02u:%02u:%02u" % (ttime.tm_hour, ttime.tm_min, ttime.tm_sec)
    # print(dat, tm)
    print(dat[5:])
    # os.system(dat)
    # os.system(tm)
    return dat[5:]  # 2019-06-23

# get_webservertime()


# vip获取网络时间
def vip_get_webservertime():
    conn = http.client.HTTPConnection('www.baidu.com')
    conn.request("GET", "/")
    r = conn.getresponse()
    # r.getheaders() #获取所有的http头
    ts = r.getheader('date')  # 获取http头date部分
    # print(ts, 11111111111111111111111111)
    # 将GMT时间转换成北京时间
    ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S")
    # print(ltime)
    ttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60)

    # print(ttime)
    dat = "date %u-%02u-%02u" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday)
    tm = "time %02u:%02u:%02u" % (ttime.tm_hour, ttime.tm_min, ttime.tm_sec)
    # print(dat, tm)
    datetime1 = dat[5:] + ' ' + tm[5:]
    # print(datetime1)
    year = int(datetime1[0:4])
    month = int(datetime1[5:7])
    day = int(datetime1[8:10])
    hour = int(datetime1[11:13])
    minute = int(datetime1[14:16])
    second = int(datetime1[17:20])
    # print(year)
    # print(month)
    # print(day)
    # print(hour)
    # print(minute)
    # print(second)

    vip_time = (datetime.datetime(year, month, day, hour, minute, second))
    # print(vip_time)
    # os.system(dat)
    # os.system(tm)
    return vip_time


def vip_default_time():
    default_time = (datetime.datetime(2000, 1, 1, 12, 00, 00))
    return default_time



# vip_default_time()
